﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public interface IGunCarrier {
    void BackFire(float value,float durtion,float lerp);
    Vector2 GetPosition();
    Vector2 GetSize();

    EMoveOri GetOri();
}
public class GunBase 
{
    protected float aspd_;

    protected IGunCarrier gc_ = null;
    protected ICameraCommonHandle ch_ = null;
    protected PrefabsMgr fa_ = null;

    public Sprite SpOfGun = null;
    public void SetGunCarrier(IGunCarrier gc) { this.gc_ = gc; }
    public void SetCameraMgr(ICameraCommonHandle ch) { this.ch_ = ch; }
    public void SetPrefabsFactory(PrefabsMgr fa) { this.fa_ = fa; }

    public void Fire() {
        var now = Clock.GetInstance().FTime;
        if (this.IsCouldFire(now)) {
           this.fire();
           this.setFireTimeStamp(now);
        }
    }

    private float fireTimeStamp_ = 0;
    private void setFireTimeStamp(float stmp) { this.fireTimeStamp_ = stmp; }
    private float getFireTimeStame() { return this.fireTimeStamp_; }

    private bool IsCouldFire(float timeNow) {
        return (timeNow - this.getFireTimeStame() > this.aspd_)&&(this.fa_ != null && this.gc_ != null && this.ch_ != null);
    }
    protected virtual void fire() {
    
    }
}

public class GunOne :GunBase{

    private float bulletSendShake_ = 0.05f;
    public GunOne() {
        this.aspd_ = 0.1f;
        bulletSendShake_ = 0.05f;
        this.SpOfGun = SpritesMgr.GetInstance().SpGun1;
    }
    protected override void fire()
    {
        this.gc_.BackFire(0.32f,0.5f,0.03f);
        var pos = this.gc_.GetPosition();

        var adiss = 0.3f;
        var xrd = UnityEngine.Random.Range(0, adiss * 2) - adiss * 2;
        var yrd = UnityEngine.Random.Range(0, adiss * 2) - adiss * 2;
        var oriValue = 1;
        if (this.gc_.GetOri() == EMoveOri.E_LEFT)
        {
            oriValue = 1;
        }
        else if (this.gc_.GetOri() == EMoveOri.E_RIGHT)
        {
            oriValue = -1;
        }
        this.fa_.Send(
               PrefabsMgr.TYPE_BULLET_CACHE,
               new WakeUpConfigBulletShell(
                   new Vector2(pos.x, pos.y + 0.5f),
                   new Vector2(1 * oriValue + xrd, 1 + yrd),
                   1000* oriValue*-1
                   ));


         adiss = bulletSendShake_;
        // xrd = UnityEngine.Random.Range(0, adiss * 2) - adiss * 2;

         var gunExitPos = new Vector2(pos.x + 0.5f * oriValue * -1, pos.y + 0.1f);
         yrd = UnityEngine.Random.Range(0, adiss * 2) - adiss ;
         this.fa_.Send(
               PrefabsMgr.TYPE_BULLET,
               new WakeUpConfigBullet(
                   gunExitPos,
                   new Vector2(1 * oriValue * -1 , 0 + yrd),
                   new BulletInfo(new Vector2(0, 0), 10,SpritesMgr.GetInstance().SpBullet1)
                   ));


        this.fa_.Send(PrefabsMgr.TYPE_GUNFIRE, new WakeUpConfigBomb(gunExitPos, new Vector2(oriValue, 0),0));

        this.ch_.Shake(0.3f,0.01f,0);
    }
}



public class GunTwo : GunBase
{

    private float bulletSendShake_ = 0.05f;
    public GunTwo()
    {
        this.aspd_ = 0.5f;
        bulletSendShake_ = 0.05f;
        this.SpOfGun = SpritesMgr.GetInstance().SpGun2;
    }
    protected override void fire()
    {
        this.gc_.BackFire(4,0.3f,0.05f);
        var pos = this.gc_.GetPosition();

        var adiss = 0.3f;
        var xrd = UnityEngine.Random.Range(0, adiss * 2) - adiss * 2;
        var yrd = UnityEngine.Random.Range(0, adiss * 2) - adiss * 2;
        var oriValue = 1;
        if (this.gc_.GetOri() == EMoveOri.E_LEFT)
        {
            oriValue = 1;
        }
        else if (this.gc_.GetOri() == EMoveOri.E_RIGHT)
        {
            oriValue = -1;
        }
        this.fa_.Send(
               PrefabsMgr.TYPE_BULLET_CACHE,
               new WakeUpConfigBulletShell(
                   new Vector2(pos.x, pos.y + 0.5f),
                   new Vector2(1 * oriValue + xrd, 1 + yrd),
                   1000 * oriValue * -1
                   ));


        adiss = bulletSendShake_;
        // xrd = UnityEngine.Random.Range(0, adiss * 2) - adiss * 2;
        var ySplitValue = 0.1f;
        var gunExitPos = new Vector2(pos.x + 0.5f * oriValue * -1, pos.y + 0.1f);
        yrd = UnityEngine.Random.Range(0, adiss * 2) - adiss;
        var sp = SpritesMgr.GetInstance().SpBullet2;
        this.fa_.Send(
              PrefabsMgr.TYPE_BULLET,
              new WakeUpConfigBullet(
                  gunExitPos,
                  new Vector2(1 * oriValue * -1, 0 + yrd + ySplitValue),
                  new BulletInfo(new Vector2(10, 1), 2.5f, sp)
                  ));

        yrd = UnityEngine.Random.Range(0, adiss * 2) - adiss;
        this.fa_.Send(
              PrefabsMgr.TYPE_BULLET,
              new WakeUpConfigBullet(
                  gunExitPos,
                  new Vector2(1 * oriValue * -1, 0 + yrd),
                  new BulletInfo(new Vector2(10, 1), 2.5f, sp)
                  ));

        yrd = UnityEngine.Random.Range(0, adiss * 2) - adiss;
        this.fa_.Send(
              PrefabsMgr.TYPE_BULLET,
              new WakeUpConfigBullet(
                  gunExitPos,
                  new Vector2(1 * oriValue * -1, 0 + yrd - ySplitValue),
                  new BulletInfo(new Vector2(10,1),2.5f, sp)
                  ));


        this.fa_.Send(PrefabsMgr.TYPE_GUNFIRE, new WakeUpConfigBomb(gunExitPos, new Vector2(oriValue, 0), 0));

        this.ch_.Shake(0.1f, 0.1f, 0);
    }
}



public class GunThree : GunBase
{

    private float bulletSendShake_ = 0.05f;
    public GunThree()
    {
        this.aspd_ = 0.1f;
        bulletSendShake_ = 0.05f;
        this.SpOfGun = SpritesMgr.GetInstance().SpGun3;
    }
    protected override void fire()
    {
        this.gc_.BackFire(1.32f, 0.5f,0.03f);
        var pos = this.gc_.GetPosition();

        var adiss = 0.3f;
        var xrd = UnityEngine.Random.Range(0, adiss * 2) - adiss * 2;
        var yrd = UnityEngine.Random.Range(0, adiss * 2) - adiss * 2;
        var oriValue = 1;
        if (this.gc_.GetOri() == EMoveOri.E_LEFT)
        {
            oriValue = 1;
        }
        else if (this.gc_.GetOri() == EMoveOri.E_RIGHT)
        {
            oriValue = -1;
        }
        this.fa_.Send(
               PrefabsMgr.TYPE_BULLET_CACHE,
               new WakeUpConfigBulletShell(
                   new Vector2(pos.x, pos.y + 0.5f),
                   new Vector2(1 * oriValue + xrd, 1 + yrd),
                   1000 * oriValue * -1
                   ));


        adiss = bulletSendShake_;
        // xrd = UnityEngine.Random.Range(0, adiss * 2) - adiss * 2;
        var ySplitValue = 0.2f;


        var gunExitPos = new Vector2(pos.x + 0.5f * oriValue * -1, pos.y + 0.1f);
        yrd = UnityEngine.Random.Range(0, adiss * 2) - adiss;
        this.fa_.Send(
              PrefabsMgr.TYPE_BULLET,
              new WakeUpConfigBullet(
                  gunExitPos,
                  new Vector2(1 * oriValue * -1, 0 + yrd + ySplitValue ),
                  new BulletInfo(new Vector2(0, 0), 10, SpritesMgr.GetInstance().SpBullet1)
                  ));

         gunExitPos = new Vector2(pos.x + 0.5f * oriValue * -1, pos.y + 0.1f);
        yrd = UnityEngine.Random.Range(0, adiss * 2) - adiss;
        this.fa_.Send(
              PrefabsMgr.TYPE_BULLET,
              new WakeUpConfigBullet(
                  gunExitPos,
                  new Vector2(1 * oriValue * -1, 0 + yrd + ySplitValue/2),
                  new BulletInfo(new Vector2(0, 0), 10, SpritesMgr.GetInstance().SpBullet1)
                  ));

        gunExitPos = new Vector2(pos.x + 0.5f * oriValue * -1, pos.y + 0.1f);
        yrd = UnityEngine.Random.Range(0, adiss * 2) - adiss;
        this.fa_.Send(
              PrefabsMgr.TYPE_BULLET,
              new WakeUpConfigBullet(
                  gunExitPos,
                  new Vector2(1 * oriValue * -1, 0 + yrd),
                  new BulletInfo(new Vector2(0, 0), 10, SpritesMgr.GetInstance().SpBullet1)
                  ));

        gunExitPos = new Vector2(pos.x + 0.5f * oriValue * -1, pos.y + 0.1f);
        yrd = UnityEngine.Random.Range(0, adiss * 2) - adiss;
        this.fa_.Send(
              PrefabsMgr.TYPE_BULLET,
              new WakeUpConfigBullet(
                  gunExitPos,
                  new Vector2(1 * oriValue * -1, 0 + yrd - ySplitValue/2),
                  new BulletInfo(new Vector2(0, 0), 10, SpritesMgr.GetInstance().SpBullet1)
                  ));
        gunExitPos = new Vector2(pos.x + 0.5f * oriValue * -1, pos.y + 0.1f);
        yrd = UnityEngine.Random.Range(0, adiss * 2) - adiss;
        this.fa_.Send(
              PrefabsMgr.TYPE_BULLET,
              new WakeUpConfigBullet(
                  gunExitPos,
                  new Vector2(1 * oriValue * -1, 0 + yrd - ySplitValue ),
                  new BulletInfo(new Vector2(0, 0), 10, SpritesMgr.GetInstance().SpBullet1)
                  ));


        this.fa_.Send(PrefabsMgr.TYPE_GUNFIRE, new WakeUpConfigBomb(gunExitPos, new Vector2(oriValue, 0), 0));

        this.ch_.Shake(0.3f, 0.05f, 0);
    }
}